package com.timothy.android;

import java.util.ArrayList;
import java.util.List;
import com.timothy.chart.CanvasSetting;
import com.timothy.chart.Cylinder;
import com.timothy.chart.VerticalBar2D;
import com.timothy.question.TypeBean1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

@Deprecated
public class LayoutAAnswerChart extends Activity {
	private final static String LOG_TAG = "LayoutAAnswerChart";
	private int screenWidth;
	private int screenHeigth ;
	private TypeBean1 bean;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		setContentView(new MyView(this));
		setContentView(new VerticalBar2DView(this));
		
		bean = TypeBean1.getMockInstance();
		
		//获取手机屏幕的宽度和高度
		WindowManager manage = this.getWindowManager(); 
		Display display = manage.getDefaultDisplay(); 
		screenWidth = display.getWidth(); 
		screenHeigth = display.getHeight();
		
		Log.i(LOG_TAG, "onCreate:screenWidth:"+screenWidth);
		Log.i(LOG_TAG, "onCreate:screenHeigth:"+screenHeigth);
	    Log.i(LOG_TAG, "onCreate:orientation"+display.getOrientation());
	}
	
//	public void onConfigurationChanged(Configuration newConfig) {
//		// TODO Auto-generated method stub
//		super.onConfigurationChanged(newConfig);
//		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//			//landscape 横屏（宽比高要长）
//			Log.i(LOG_TAG, "onConfigurationChanged:landscape");
//			setContentView(new LandscapeView(this));
//		}else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//			//portrait 竖屏(高比宽要长)
//			Log.i(LOG_TAG, "onConfigurationChanged:portrait");
//			setContentView(new MyView(this));
//		}
//		
//		bean = TypeBean1.getMockInstance();
//		//重新获取手机屏幕的宽度和高度
//		WindowManager manage = this.getWindowManager(); 
//		Display display = manage.getDefaultDisplay(); 
//		screenWidth = display.getWidth(); 
//		screenHeigth = display.getHeight();
//		Log.i(LOG_TAG, "onConfigurationChanged:screenWidth:"+screenWidth);
//		Log.i(LOG_TAG, "onConfigurationChanged:screenHeigth:"+screenHeigth);
//	}

	class MyView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public MyView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			Log.i(LOG_TAG, "MyView:Canvas Height:"+canvas.getHeight());
			Log.i(LOG_TAG, "MyView:Canvas Width:"+canvas.getWidth());
			
			canvas.drawColor(Color.BLACK);// 设置画布背景为黑色
			Paint mPaint = new Paint();
			
			mPaint.setColor(Color.WHITE);		
//			canvas.drawText("screenWidth:"+String.valueOf(screenWidth), 50, 50, mPaint); 
//			canvas.drawText("screenHeigth:"+String.valueOf(screenHeigth), 50, 75, mPaint); 
			
			mPaint.setTextSize(35);
			canvas.drawText(bean.getQuestion(), 50, 75, mPaint); 
			canvas.save();

			//得到所有柱形的信息
			List<Cylinder> cyliList = new ArrayList<Cylinder>();
			Cylinder cylinder1 = new Cylinder(0,250,Color.RED,"YES:300");
			
			cylinder1.setTopColor(Color.parseColor("#FF0000"));
			cylinder1.setBottomColor(Color.parseColor("#D40000"));
			
			Cylinder cylinder2 = new Cylinder(0,400,Color.GREEN,"NO:400");
			cylinder2.setTopColor(Color.GREEN);
			cylinder2.setBottomColor(Color.parseColor("#0C9720"));
			
			cyliList.add(cylinder1);
			cyliList.add(cylinder2);
			
			//先设置画布
			CanvasSetting setting = CanvasSetting.getMockInstance();
			setting.setTopMargin(200);
			setting.setBottomMargin(200);
			setting.setCountOfCylinder(cyliList.size());
			
//			setting.setTopMargin(screenHeigth/10);
//			setting.setBottomMargin(screenHeigth/10);
			
			int canvasWidth = screenWidth - setting.getLeftMargin() - setting.getRightMargin();
			double widthOfCylinder = (canvasWidth - setting.getSpaceInCylis() * (setting.getCountOfCylinder()-1))/setting.getCountOfCylinder();
			setting.setWidthOfCylinder((int)widthOfCylinder);
			

			
			for(int i=0;i<cyliList.size();i++){
				Cylinder cylinder = cyliList.get(i);
				
				int tlX;
				int tlY;
				int brX;
				int brY;
				
				if(i==0){
					tlX = setting.getLeftMargin();
					tlY = screenHeigth - setting.getBottomMargin() - cylinder.getHeight();
					brX = setting.getLeftMargin() + setting.getWidthOfCylinder();
					brY = screenHeigth - setting.getBottomMargin();
				}else{
					tlX = (int)(setting.getLeftMargin() + i * setting.getWidthOfCylinder() + setting.getSpaceInCylis()* i);
					tlY = screenHeigth - setting.getBottomMargin() - cylinder.getHeight();
					brX = (int)(setting.getLeftMargin() + (i + 1)* setting.getWidthOfCylinder()+ setting.getSpaceInCylis()* i);
					brY = screenHeigth - setting.getBottomMargin();					
				}
				
				mPaint.setTextSize(25);
				
				canvas.drawText(cylinder.getChoiceText(), tlX, tlY-20, mPaint);
				
				mPaint.setColor(cylinder.getTopColor());//圆柱顶部颜色
				canvas.drawOval(new RectF(tlX,tlY-20,brX,tlY+20), mPaint);
				
				mPaint.setColor(cylinder.getColor());//圆柱中间颜色
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				
				mPaint.setColor(cylinder.getBottomColor());//圆柱底部颜色
				canvas.drawOval(new RectF(tlX,brY-20,brX,brY+20), mPaint);
				
				canvas.save();
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}
	
	class VerticalBar2DView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public VerticalBar2DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalBar2DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalBar2DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
		
			int totalVotes = 0;
			
			//得到传送数据
			List<VerticalBar2D> barList = new ArrayList<VerticalBar2D>();
			VerticalBar2D bar1=new VerticalBar2D(0,30,"Yes",2,Color.RED);
			totalVotes+=bar1.getVotes();
			barList.add(bar1);
			VerticalBar2D bar2=new VerticalBar2D(1,70,"No",2,Color.GREEN);
			totalVotes+=bar2.getVotes();
			barList.add(bar2);
			VerticalBar2D bar3=new VerticalBar2D(1,100,"NoSure",2,Color.YELLOW);
			totalVotes+=bar3.getVotes();
			barList.add(bar3);
			Log.i(LOG_TAG, "VerticalBar2DView:totalVotes:"+totalVotes);
			
			//write question
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			Paint mPaint = new Paint();
			mPaint.setColor(Color.WHITE);		
			mPaint.setTextSize(35);
			canvas.drawText(bean.getQuestion(), 50, 75, mPaint); 
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "VerticalBar2DView:spaceInCharts:"+setting.getSpaceInCylis());
			int barWidth = (canvasWidth - setting.getLeftMargin() - setting.getRightMargin())/barList.size() - (int) setting.getSpaceInCylis();
			Log.i(LOG_TAG, "VerticalBar2DView:barWidth:"+barWidth);
			
			for(int i=0;i<barList.size();i++){
				VerticalBar2D bar2D = barList.get(i);
				
				//根据总票数，计算出比例，再乘以画布的高度
				
				int votes = bar2D.getVotes();
				double votesShare = (1.0* bar2D.getVotes())/totalVotes;
				int barHeigth = (int) (votesShare * canvasHeight);
				
				Log.i(LOG_TAG, "VerticalBar2DView:barHeigth:"+barHeigth);
				Log.i(LOG_TAG, "VerticalBar2DView:votes:"+votes);
				Log.i(LOG_TAG, "VerticalBar2DView:votesShare:"+votesShare);
				
				String answerText = null; 
				switch (bar2D.getAnswerType()){
				    case 0:answerText =  String.valueOf(votes);break; 
				    case 1:answerText =  String.valueOf(votesShare*100)+"%";break;
				    case 2:answerText =  String.valueOf(votes)+":"+String.valueOf(votesShare*100)+"%";break;
				}
				
				Log.i(LOG_TAG, "VerticalBar2DView:answerText:"+answerText);
				
				int tlX = (int) (setting.getLeftMargin()+ i * (barWidth + setting.getSpaceInCylis()));
				int tlY =  canvasHeight - setting.getBottomMargin() - barHeigth;
				int brX = tlX + barWidth;
				int brY = canvasHeight - setting.getBottomMargin();
				
				Log.i(LOG_TAG, "VerticalBar2DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
				
				mPaint.setTextSize(25);
				canvas.drawText(answerText, tlX, tlY-20, mPaint);
				canvas.drawText(bar2D.getChoiceText(), tlX, canvasHeight-20, mPaint);
				
				mPaint.setColor(bar2D.getFullColor());
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				
				canvas.save();
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			
		}
	}
	
	class LandscapeView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public LandscapeView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			canvas.drawColor(Color.BLUE);// 设置画布背景为黑色
			Paint mPaint = new Paint();
			
			//得到所有柱形的信息
			List<Cylinder> cyliList = new ArrayList<Cylinder>();
			Cylinder cylinder1 = new Cylinder(0,250,Color.RED,"YES:300");
			
			cylinder1.setTopColor(Color.parseColor("#FF0000"));
			cylinder1.setBottomColor(Color.parseColor("#D40000"));
			
			Cylinder cylinder2 = new Cylinder(0,400,Color.GREEN,"NO:400");
			cylinder2.setTopColor(Color.GREEN);
			cylinder2.setBottomColor(Color.parseColor("#0C9720"));
			
			cyliList.add(cylinder1);
			cyliList.add(cylinder2);
			
			//先设置画布
			CanvasSetting setting = CanvasSetting.getMockInstance();
			setting.setCountOfCylinder(cyliList.size());
			
//			setting.setTopMargin(screenHeigth/10);
//			setting.setBottomMargin(screenHeigth/10);
			
			mPaint.setColor(Color.WHITE);		
			mPaint.setTextSize(25);
			canvas.drawText(bean.getQuestion(), setting.getLeftMargin(), setting.getTopMargin()/2, mPaint); 
			canvas.save();
			
			//80 is title length which on the top of canvas 
			int canvasWidth = screenHeigth - 80 - setting.getTopMargin() - setting.getBottomMargin();
			Log.i(LOG_TAG,"LandscapeView:"+ "canvasWidth="+canvasWidth);
			Log.i(LOG_TAG,"LandscapeView:"+ "SpaceInCylis="+setting.getSpaceInCylis());
			double widthOfCylinder = (canvasWidth - setting.getSpaceInCylis() * (setting.getCountOfCylinder()-1))/setting.getCountOfCylinder();
			Log.i(LOG_TAG,"LandscapeView:"+ "widthOfCylinder="+widthOfCylinder);
			setting.setWidthOfCylinder((int)widthOfCylinder);
			
			for(int i=0;i<cyliList.size();i++){
				Cylinder cylinder = cyliList.get(i);
				
				int tlX;
				int tlY;
				int brX;
				int brY;
				
				if(i==0){
					tlX = setting.getLeftMargin();
					tlY = setting.getTopMargin();
					brX = setting.getLeftMargin()+cylinder.getHeight();
					brY = setting.getTopMargin()+ setting.getWidthOfCylinder();
				}else{
					tlX = setting.getLeftMargin();
					tlY = setting.getTopMargin()+ setting.getWidthOfCylinder()*i +(int) setting.getSpaceInCylis()*i;
					brX = setting.getLeftMargin()+cylinder.getHeight();
					brY = setting.getTopMargin() + setting.getWidthOfCylinder()*(i+1)+ (int) setting.getSpaceInCylis()*i;				
				}
				
				mPaint.setColor(cylinder.getColor());//圆柱中间颜色
				Log.i(LOG_TAG,"LandscapeView:"+ "tlX="+tlX+" tlY="+tlY+" brX="+brX+" brY="+brY);
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				canvas.save();
				
				//new new new
//				List<VerticalBar2D> bar2DList = new ArrayList<VerticalBar2D>();
//				for(int i=0;i<bar2DList.size();i++){
//					
//				}
				
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}
}
